<template>

  <div>
    <div class="panel">
      <panel-title title="">
        <el-button type="success" icon="plus" size="mini" @click="toList()">返回列表</el-button>
      </panel-title>
    </div>

    <div class="panel">
      <div class="panel-body">
        <el-form label-position="right" label-width="20%" :rules="rules" :model="product" ref="productForm" size="mini">


          <el-form-item label="免费限购">
            <el-switch v-model="product.freeLimit" active-value="1"
                       inactive-value="2"></el-switch>
          </el-form-item>
          <el-form-item label="代理价格">
            <el-switch v-model="product.agentEnable" active-value="1"
                       inactive-value="2"></el-switch>
          </el-form-item>
          <el-form-item label="批发价格">
            <el-switch v-model="product.wholesaleEnable" active-value="1"
                       inactive-value="2"></el-switch>
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="product.order" style="width: 200px" placeholder="排序，越大越靠前"></el-input>
          </el-form-item>

          <el-form-item label="名称">
            <el-input v-model="product.name" style="width: 400px" placeholder="商品名称"></el-input>
          </el-form-item>

          <el-form-item label="价格">
            <el-input v-model="product.price" style="width: 400px" type="number" step="0.01"></el-input>
          </el-form-item>
          <el-form-item label="原价">
            <el-input v-model="product.firstPrice" style="width: 400px" type="number" step="0.01"></el-input>
          </el-form-item>

          <el-form-item label="存量">
            <el-input v-model="product.stock" style="width: 400px" type="number" step="1"></el-input>
          </el-form-item>
          <el-form-item label="编码">
            <el-input v-model="product.code" style="width: 400px"></el-input>
          </el-form-item>
          <el-form-item label="描述说明">

            <el-input type="textarea" v-model="product.desc" style="width: 400px"></el-input>
          </el-form-item>
          <el-form-item label="售后描述说明">
            <el-input type="textarea" v-model="product.descAfter" style="width: 400px"></el-input>
          </el-form-item>
        </el-form>
        <el-row style="text-align: center">
          <el-button type="success" @click="save('productForm')" :disabled="addFlag">保存
          </el-button>
        </el-row>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
  import {panelTitle, bottomToolBar} from 'components'

  export default {
    data() {
      return {
        rules: {},
        product: {},
      }
    },
    components: {
      panelTitle,
      bottomToolBar

    },
    methods: {

      timeFormatter(time, column) {
        if (!time) {
          return "";
        }
        return this.$dateFormat(time, "yyyy-MM-dd hh:mm")

      },
      save(form) {
        if (this.addFlag) {
          return;
        }
        this.addFlag = true;
        this.$fetch.api_product.add(this.product)
          .then(() => {
            //添加成功
            // this.$message.success("操作成功")
            this.addFlag = false;
            this.goList();

          })
          .catch(({message: message}) => {
            this.$notify.error({
              title: '错误信息',
              message: message,
              showClose: true
            });
            this.addFlag = false;
          })

      }
    },
    goList() {
      this.$router.push({path: '/product/list'})
    },
    mounted() {

    }
  }
</script>
